#include "account_info.h"
#include "ui_acconet_info.h"
#include <QDialogButtonBox>
#include <QLineEdit>
#include <QFileDialog>
#include "logs.h"

Account_INFO::Account_INFO(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Acconet_INFO)
{
    ui->setupUi(this);
    this->setWindowFlag(Qt::FramelessWindowHint);
    this->setAttribute(Qt::WA_TranslucentBackground,true);

    QVariant var = JsonConfigManager::instance()->get("name",QVariant("root"));
    ui->name->setText(var.toString());

    QVariant pixpath = JsonConfigManager::instance()->get("path",QVariant("./"));
    qDebug()<<"pixpath:"<<pixpath.toString();
    QPixmap pix;
    pix.load(pixpath.toString());

    pix = pix.scaled(
        ui->label->width(),
        ui->label->height(),
        Qt::KeepAspectRatio,  // 保持宽高比
        Qt::SmoothTransformation  // 平滑缩放
    );
     ui->label->setPixmap(pix);
     ui->label->setStyleSheet("background-color:rgb(255, 255, 255);border-radius:50px;");

    QVariant sex =  JsonConfigManager::instance()->get("sex","true");
    bool isman{sex.toBool()};
    if(isman)
    {
        ui->man->setChecked(true);
    }else
    {
        ui->woman->setChecked(true);
    }

   // ui->pixWidget->setWindowFlag(Qt::FramelessWindowHint);
   // ui->pixWidget->setAttribute(Qt::WA_TranslucentBackground,true);

}

Account_INFO::~Account_INFO()
{
    delete ui;
}

bool Account_INFO::getEnter() const
{
    return isEnter;
}

void Account_INFO::enterEvent(QEvent *event)
{
   isEnter=true;

}

void Account_INFO::leaveEvent(QEvent *event)
{
     isEnter =false;
}

void Account_INFO::on_changeName_clicked()
{
    QDialog dialog;
    dialog.setWindowTitle("Change Name");
    dialog.setFixedSize(250, 100);
    dialog.setWindowFlag(Qt::FramelessWindowHint);
    QVBoxLayout *layout = new QVBoxLayout(&dialog);

    QLineEdit *nameEdit = new QLineEdit(&dialog);
    nameEdit->setPlaceholderText("输入新名字");
    layout->addWidget(nameEdit);
    QLabel *tips=new QLabel(&dialog);
    layout->addWidget(tips);

    QDialogButtonBox *buttonBox = new QDialogButtonBox(
        QDialogButtonBox::Ok | QDialogButtonBox::Cancel,
        &dialog
    );

    layout->addWidget(buttonBox);

    connect(nameEdit,&QLineEdit::cursorPositionChanged,&dialog,[&](){
            tips->setText("");
       if(nameEdit->text().size()< 3)
       {
           tips->setStyleSheet("color:red");
           tips->setText("   名称长度不够");
       }

    });

    // 连接按钮信号
    connect(buttonBox, &QDialogButtonBox::accepted, &dialog, &QDialog::accept);
    connect(buttonBox, &QDialogButtonBox::rejected, &dialog, &QDialog::reject);

    // 显示对话框并等待结果
    if (dialog.exec() == QDialog::Accepted)
    {
       QString name = nameEdit->text();
       if(name.size()<3 || name.size() >12)
       {
           return;
       }

      JsonConfigManager::instance()->get("name",name);
      ui->name->setText(name);
    JsonConfigManager::instance()->set("name",name);
      //JsonConfigManager::instance()->save();
      // qDebug() << "New name accepted:" << nameEdit->text();
        // 这里执行实际更改名称的操作
    }
}


void Account_INFO::on_changePix_clicked()
{

    QVariant var=("/");
    QString str("path");
    //增加JSon解析时需要依照Json去获得图片路径
    QString path = JsonConfigManager::instance()->get("path",var).toString();
    QString FilePath;
    FilePath=QFileDialog::getOpenFileName(this,"选择图片", path,tr("Image Files(*.png *.jpg *.bmp *.webp)"));
    if(FilePath.isEmpty())
    {
        return ;
    }
    JsonConfigManager::instance()->set("path",QVariant(FilePath));
    JsonConfigManager::instance()->save();
    QFile pix(FilePath);
    if(!pix.open(QIODevice::ReadOnly))
    {
        qWarning()<<"picture open file";
    }

    auto bty = pix.readAll();
    QPixmap PIX;

    PIX.loadFromData(bty);
    PIX = PIX.scaled(
        ui->label->width(),
        ui->label->height(),
        Qt::KeepAspectRatio,  // 保持宽高比
        Qt::SmoothTransformation  // 平滑缩放
    );
    //QFileDialog pixfile(this,"selectPix",path,tr("Image Files(*.png *.jpg *.bmp *.)"));
    ui->label->setPixmap(PIX);
    ui->label->setStyleSheet("background-color:rgb(255, 255, 255);border-radius:50px;");

}

void Account_INFO::on_exec_clicked()
{

    this->close();
   emit closeAccount();
}

void Account_INFO::on_man_clicked()
{
    JsonConfigManager::instance()->set("sex","true");
    JsonConfigManager::instance()->save();
}

void Account_INFO::on_woman_clicked()
{
    JsonConfigManager::instance()->set("sex","false");
     JsonConfigManager::instance()->save();
}
